#include "mongoclient.h"

vector<BSONObj> MongoClient::query_v(const char* collection, BSONObj obj, bool getid){
    vector<BSONObj>        v;
    auto_ptr<DBClientCursor> cursor = DBClientConnection::query(collection, obj);
    while(cursor->more()){
        BSONObj obj = cursor->next();
        if(getid){
            v.push_back(obj);
        }
        else{
            v.push_back(obj.removeField("_id"));
        }
    }
    return v;
}

BSONObj MongoClient::query(const char* collection, BSONObj obj, bool getid){
    BSONArrayBuilder builder;

    auto_ptr<DBClientCursor> cursor = DBClientConnection::query(collection, obj);
    while(cursor->more()){
        BSONObj obj = cursor->next();
        if(getid){
            builder.append(obj);
        }
        else{
            builder.append(obj.removeField("_id"));
        }
    }
    return builder.obj();
}

BSONObj MongoClient::query_one(const char* collection, BSONObj query, bool getid){
    auto_ptr<DBClientCursor> cursor = DBClientConnection::query(collection, query);
    BSONObj obj;
    if(cursor->more()){
        if(getid){
            obj = cursor->next();
        }
        else{
            obj = cursor->next().removeField("_id");
        }
    }

    return obj;
}
